package com.example.app05.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app05.MainActivity;
import com.example.app05.R;
import com.example.app05.dao.MyDatabaseHelper;
import com.example.app05.dao.TaskDao;
import com.example.app05.entity.Task;
import com.example.app05.tool.Tool;

import java.text.SimpleDateFormat;

/**
 * @author: cx
 * @date: 2022/5/1
 */
public class UpdateActivity extends AppCompatActivity {

    private Task task;

    //创建数据库所用参数
    private SQLiteDatabase db;
    private MyDatabaseHelper dbHelper;

    //页面控件
    private Button btnBack, btnUpdate;
    private EditText etTopic, etDoTime, etContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_update);
        receiveData();
        initDatabaseByDBHelper();
        findView();
        showMessage();
        setListener();
    }
    private void findView(){
        btnBack = findViewById(R.id.tu_btnBack);
        btnUpdate = findViewById(R.id.tu_btnUpdate);
        etTopic = findViewById(R.id.tu_topic);
        etDoTime = findViewById(R.id.tu_time);
        etContent = findViewById(R.id.tu_content);
    }
    private void showMessage(){
        etTopic.setText(task.getTopic());
        etDoTime.setText(task.getDoTime());
        etContent.setText(task.getContent());
    }
    private void setListener(){
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tool.onItemClick(UpdateActivity.this, MainActivity.class,null);
                UpdateActivity.this.finish();
            }
        });
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String newTime = sdf.format(System.currentTimeMillis());
                Task newTask = new Task(task.getId(),etTopic.getText().toString(),etDoTime.getText().toString()
                        ,task.getShowTime(),newTime,etContent.getText().toString());
                long dataId = TaskDao.updateData(db,newTask,task.getId());
                if(dataId > 0){
                    Toast.makeText(
                            getApplicationContext(),
                            "修改数据成功",
                            Toast.LENGTH_SHORT
                    ).show();
                    Tool.onItemClick(UpdateActivity.this,MainActivity.class,null);
                    UpdateActivity.this.finish();
                }else{
                    Toast.makeText(
                            getApplicationContext(),
                            "修改数据失败",
                            Toast.LENGTH_SHORT
                    ).show();
                }
            }
        });
    }
    //创建数据库
    public void initDatabaseByDBHelper() {
        //创建数据库管理类的对象
        dbHelper = new MyDatabaseHelper(
                this,"task_table",null,1
        );
        //获得数据库对象
        db = dbHelper.getWritableDatabase();
    }
    //接收任务列表页面跳转过来的数据
    private void receiveData(){
        Bundle bundle= (Bundle) getIntent().getExtras().get("bundle");
        task = (Task) bundle.getSerializable("task");
    }
}
